package com.qkl.upload.publics.web.controller;

import com.qkl.upload.publics.service.UploadFileService;
import com.qkl.upload.publics.utils.Result;
import com.qkl.upload.publics.utils.UploadFileDTO;
import com.qkl.upload.publics.utils.UploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("{root.baseURL}")
@Slf4j
public class UploadController {

    @Autowired
    private UploadFileService uploadFileService;


    /**
     * 上传文件
     * @param request
     * @param response
     * @return
     */
    @PostMapping(value = "/uploadFile")
    public Result uploadFile(HttpServletRequest request, HttpServletResponse response){
        UploadFileDTO fileDTO = UploadUtils.parseFile(request);
        fileDTO.setUploadType(1);
        return ControllerHelper.toOperateResult(uploadFileService.uploadFile(fileDTO));
    }

    /**
     * 上传文件养殖视频
     * @param request
     * @param response
     * @return
     */
    @PostMapping(value = "/uploadBreedVideo")
    public Result uploadBreedVideo(HttpServletRequest request, HttpServletResponse response){
        UploadFileDTO fileDTO = UploadUtils.parseFile(request);
        fileDTO.setUploadType(2);
        Map<String,Object> params = fileDTO.getParams();
        if(null == params){
            params = new HashMap<>();
        }
        params.put("title","养殖视频");
        params.put("titleSub","养殖过程视频");
        params.put("type",1);
        //params.put("typeSub","");
        fileDTO.setParams(params);
        return ControllerHelper.toOperateResult(uploadFileService.uploadFile(fileDTO));
    }

    /**
     * 共公文件删除
     * @param params
     * @return
     */
    @GetMapping (value = "/deleteCommonFile")
    public Result deleteCommonFile(@RequestParam Map<String,Object> params){
        String uploadId = (String) params.get("uploadId");
        return ControllerHelper.toOperateResult(uploadFileService.deleteCommonFile(uploadId));
    }

    @GetMapping (value = "/uploadFileTest")
    public Result uploadFileTest(HttpServletRequest request, HttpServletResponse response){
        return Result.success();
    }



}
